package com.hsd.znsh.controller.app;

import com.hsd.znsh.entity.Gateway;
import com.hsd.znsh.enums.OnlineStatusEnum;
import com.hsd.znsh.pojo.output.GatewayVO;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.service.GatewayService;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-08-02 15:13:34
 */
@RestController
@RequestMapping("/app/gateway")
public class AppGatewayController {

    @Autowired
    private GatewayService gatewayService;

    @PostMapping("/gatewayInvalid")
    public ResultVO gatewayInvalid(Gateway gateway, Integer code){
        System.out.println("gatewayInvalid");
        if(code!=0) gateway.setOnlineStatus(OnlineStatusEnum.NOT_ONLINE.getCode());
        else gateway.setOnlineStatus(OnlineStatusEnum.ONLINE.getCode());
        gatewayService.updateGatewayStatus(gateway);
        return ResultVOUtil.success();
    }

    @GetMapping("/getGatewayInfoByGatewayId")
    public ResultVO getGatewayInfoByGatewayId(String gatewayId){
        GatewayVO gatewayVO=gatewayService.getGatewayInfoByGatewayId(gatewayId);
        return ResultVOUtil.success(gatewayVO);
    }

    @PostMapping("/editGateway")
    public ResultVO editGateway(@Valid Gateway gateway, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        if(gateway.getId()==null){
            gatewayService.addGateway(gateway);
            return ResultVOUtil.success("添加成功");
        }else{
            gatewayService.updateGateway(gateway);
            return ResultVOUtil.success("修改成功");
        }
    }

    @PostMapping("/deleteGateway")
    public ResultVO deleteGateway(String gatewayId){
        gatewayService.deleteGateway(gatewayId);
        return ResultVOUtil.success("删除成功");
    }

    @GetMapping("/getGatewayListByHostSn")
    public ResultVO getGatewayListByHostSn(String hostSn){
        List<Gateway> list=gatewayService.getGatewayListByHostSn(hostSn);
        return ResultVOUtil.success(list);
    }
}
